package elitech.vietnam.weatherfashion.ultilities;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.Utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

@SuppressLint("WorldWriteableFiles")
public class FileOption {
	private final static String TAG = FileOption.class.getSimpleName();

	private Context context;

	public static String PREF_FILE_NAME = "weather-fashion.ini";
	public final static String LANGUAGE = "language";
	public final static String USERNAME = "username";
	public final static String PASS = "pass";
	public final static String IMAGE_HOST_URL = "image_host_url";

	public final static String AUTO_LOGIN = "auto_login";
	public final static String NOTIFICATION = "notification";
	public final static String LOCATION = "location";

	public final static String LANGUAGE_CODE = "language";
	public final static String LANGUAGE_INDEX = "language_index";

	private Properties props;

	public FileOption(Context context) {
		this.context = context;
		PREF_FILE_NAME = "weather-fashion.ini";
		boolean isExist = true;
		try {
			context.openFileInput(PREF_FILE_NAME);
		} catch (FileNotFoundException e) {
			Log.e(TAG, "can't read pref file", e);
			isExist = false;
		}
		props = new Properties();
		if (!isExist) {
			reconstruct();
		}
		props.clear();

		FileInputStream fin;
		try {
			fin = context.openFileInput(PREF_FILE_NAME);
			props.load(fin);
			fin.close();
		} catch (FileNotFoundException e) {
			Log.e(TAG, "can't read pref file", e);
		} catch (IOException e) {
			Log.e(TAG, "can't read pref file", e);
		}
	}

	/**
	 * write value option to file
	 * 
	 * @param option
	 *            Value need save
	 */
	public void WriteOption(ValueOption option) {
		
		Utils.Log("WriteOption - LanguageIndex " + option.getLanguageIndex());
		Utils.Log("WriteOption - LanguageCode " + option.getLanguageCode());
		
		if (option == null)
			return;
		try {
			FileOutputStream fos = context.openFileOutput(PREF_FILE_NAME,
					Context.MODE_WORLD_WRITEABLE);
			props.setProperty(USERNAME, option.getUsername());
			props.setProperty(PASS, option.getPassword());
//			props.setProperty(IMAGE_HOST_URL, option.getImageHostUrl());

			props.setProperty(LOCATION, String.valueOf(option.isGetLocation()));

			if (option.isAutoLogin() == false)
				props.setProperty(AUTO_LOGIN, "0");
			else
				props.setProperty(AUTO_LOGIN, "1");

			if (option.isGetNotification() == false)
				props.setProperty(NOTIFICATION, "0");
			else
				props.setProperty(NOTIFICATION, "1");

			if (option.isGetLocation() == false)
				props.setProperty(LOCATION, "0");
			else
				props.setProperty(LOCATION, "1");

			props.setProperty(LANGUAGE_CODE, option.getLanguageCode());
			props.setProperty(LANGUAGE_INDEX,
					String.valueOf(option.getLanguageIndex()));

			props.store(fos, "weather-fashion");
			fos.close();
		} catch (Exception e) {
			// the file could be crashed, so reconstruct one more
		}
	}

	/**
	 * Read info option from file
	 * 
	 * @return ValueOption
	 */
	public ValueOption ReadOption() {
		ValueOption option = new ValueOption();
		try {
			FileOutputStream fos = context.openFileOutput(PREF_FILE_NAME,
					Context.MODE_WORLD_WRITEABLE);
			option.setUsername(props.getProperty(USERNAME, ""));
			option.setPassword(props.getProperty(PASS, ""));
			// option.setImageHostUrl(props.getProperty(IMAGE_HOST_URL,
			// "http://eli8w.cafe24.com/fashion/"));

			int i = Integer.parseInt(props.getProperty(LOCATION, "1"));
			if (i == 0)
				option.setGetLocation(false);
			else
				option.setGetLocation(true);

			i = Integer.parseInt(props.getProperty(AUTO_LOGIN, "1"));
			if (i == 0)
				option.setAutoLogin(false);
			else
				option.setAutoLogin(true);

			i = Integer.parseInt(props.getProperty(NOTIFICATION, "1"));
			if (i == 0)
				option.setGetNotification(false);
			else
				option.setGetNotification(true);

			option.setLanguageCode(props.getProperty(LANGUAGE_CODE, ""));
			option.setLanguageIndex(Integer.parseInt(props.getProperty(
					LANGUAGE_INDEX, "0")));

			props.store(fos, "weather-fashion");
			fos.close();
			return option;
		} catch (Exception e) {
			return new ValueOption();
		}

	}

	private void reconstruct() {
		try {
			Log.d(TAG, "reconstruct");
			FileOutputStream fos = null;
			fos = context.openFileOutput(PREF_FILE_NAME,
					Context.MODE_WORLD_WRITEABLE);
			/*
			 * props.setProperty(TIME_KEY, System.currentTimeMillis()+"");
			 * props.setProperty(KEYWORD_KEY, "science"); props.store(fos,
			 * "reconstrut version");
			 */
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "can't create pref file", e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "can't create pref file", e);
		}
	}
}
